package kibahed.university.effizientealgorithmen.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;

import kibahed.university.effizientealgorithmen.interfaces.IAlgorithm;
import kibahed.university.effizientealgorithmen.interfaces.IGraph;

public class StartFrame extends JFrame{
	
	private IGraph graph=null;
	
	private JPanel mainPanel = new JPanel();
	
	private JPanel graphPanel = new JPanel();
	
	private JPanel consolePanel = new JPanel();
	
	private JScrollPane consoleScroll = new JScrollPane();
	
	private JTextArea console = new JTextArea();
	
	private ArrayList<IAlgorithm> algorithms = new ArrayList<IAlgorithm>();
	
	private GraphVizualisation graphVisualisation = new GraphVizualisation();
	
	
	public static void main(String[] args){
		StartFrame startFrame = new StartFrame();
	}
	
	public StartFrame(){
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		addMenu();
		showFrame();
	}
	
	private void showFrame(){
		this.setContentPane(mainPanel);
		pack();
		this.setSize(1400, 900);
		setVisible(true);
		
	}
	
	private void addMenu(){
		this.setJMenuBar(new Menu(this));
	}

	public void setGraph(IGraph graph) {
		
		this.graph=graph;
		graphVisualisation.setGraph(graph,this);
		formatPanels();
		fillPanels();
		update();
	}
	
	public void update(){
		
		mainPanel.updateUI();
		graphPanel.updateUI();
		graphVisualisation.updateUI();
		console.setText(graph.getMessages());
		console.validate();
		console.setSize(new Dimension(100,12*graph.getMessages().split("\n").length));
		consolePanel.validate();
		this.validate();
	}
	
	private void formatPanels(){
		this.setLayout(new BorderLayout());
		mainPanel.setLayout(new BorderLayout());
		graphPanel.setLayout(new BorderLayout());
		graphPanel.setBackground(Color.WHITE);
		consolePanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
		consolePanel.setLayout(new BorderLayout());
		consolePanel.setMinimumSize(new Dimension(100,100));
		consolePanel.setPreferredSize(new Dimension(100,100));
		consolePanel.setMaximumSize(new Dimension(1200,100));
		console.setAutoscrolls(true);
	}
	
	private void fillPanels(){
		mainPanel.add(graphPanel,BorderLayout.CENTER);
		graphPanel.add(graphVisualisation,BorderLayout.CENTER);
		graphVisualisation.addZoomListener(this);
		mainPanel.add(consolePanel,BorderLayout.SOUTH);
		consolePanel.add(consoleScroll, BorderLayout.CENTER);
		consoleScroll.setViewportView(console);
	}
	
	
	public IGraph getGraph(){
		return graph;
	}
	
	public ArrayList<IAlgorithm> getAlgorithms(){
		return algorithms;
	}
	
	public void addAlgorithm(IAlgorithm algorithm){
		algorithms.add(algorithm);
	}
	
	public void removeAlgorithm(IAlgorithm algorithm){
		algorithms.remove(algorithm);
	}
	

}
